package com.zhan.ielts.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.CustomResult;
import com.zhan.ielts.domain.UnknownVocabulary;
import com.zhan.ielts.domain.Users;
import com.zhan.ielts.domain.query.UnknownVocabularyQuery;
import com.zhan.ielts.service.UnknownVocabularyService;
import com.zhan.ielts.service.UsersService;

@Controller
@RequestMapping("/unknown/vocabulary")
public class UnknownVocabularyController extends BaseControllerImpl<UnknownVocabulary, UnknownVocabularyQuery> {

	private Logger log = LoggerFactory.getLogger(UnknownVocabularyController.class);

	@Autowired
	private UnknownVocabularyService unknownVocabularyService;

	@Autowired
	private UsersService usersService;

	@Override
	protected BaseService<UnknownVocabulary> getBaseService() {
		return unknownVocabularyService;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ResponseBody
	@RequestMapping(value = "/fetch", method = RequestMethod.POST)
	public CustomResult fetchUnknownVocabularys(UnknownVocabularyQuery data) {
		log.debug("获取生词本信息开始");

		Map map = new HashMap();
		// 检查userId 是否为空
		if (StringUtils.isEmpty(data.getUserId())) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}

		try {
			// 检查userId 是否有效
			Users query = new Users();
			query.setId(data.getUserId());
			if (usersService.queryCount(query) < 1) {
				map.put("errCode", 1002);
				return new CustomResult(0, "userId无效！", map);
			}
			map = unknownVocabularyService.fetchUnknownVocabulary(data);
			log.debug("获取生词本信息结束");
		} catch (Exception e) {
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误", map);
		}
		log.debug("获取生词本信息结束");
		return new CustomResult(1, "", map);
	}

	@SuppressWarnings({ "unchecked", "deprecation", "unused", "rawtypes" })
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public CustomResult updateUnknownVocabulary(String userId, String newWords, String device, String osVersion, String appVersion) {
		log.debug("更新生词本开始");
		Map map = new HashMap();

		// 检查userId 是否为空
		if (StringUtils.isEmpty(userId)) {
			map.put("errCode", 1001);
			return new CustomResult(0, "userId为空！", map);
		}

		try {
			// 检查userId 是否有效
			Users query = new Users();
			query.setId(userId);
			if (usersService.queryCount(query) < 1) {
				map.put("errCode", 1002);
				return new CustomResult(0, "userId无效！", map);
			}

			JSONArray jan = JSONArray.fromObject(newWords);
			List<UnknownVocabulary> unknownVocabularyList = JSONArray.toList(jan, UnknownVocabulary.class);

			// 更新答案
			unknownVocabularyService.updateUnknownVocabulary(unknownVocabularyList, userId, device, osVersion, appVersion);
		} catch (Exception e) {
			map.put("errCode", 1003);
			return new CustomResult(0, "系统错误！", map);
		}

		log.debug("更新生词本结束");
		return new CustomResult(1, "", map);
	}

}
